package com.jcnet.bizaia.view;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.DiscussDetialActivity;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.bean.HeadCommedBean;
import com.jcnet.bizaia.bean.HeadCommedHttpDataBean;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.xstream.XmlCacheHelper;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;
import com.viewpagerindicator.CirclePageIndicator;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class HeadViewPageView extends FrameLayout {

	private static final String HEAD_DATA = "head_data";  //缓存的数据

	private Context context;

	private ViewPager pager;

	private List<HeadCommedBean> headCommedBeans;

	private CirclePageIndicator indicator;

	private int category = 1;

	private ImagePagerAdapter pagerAdapter;

	private XmlCacheHelper xmlCacheHelper;

	private AsyncHttpClient asyncHttpClient;

	public HeadViewPageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		initView();
	}


	public HeadViewPageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.context = context;
		initView();
	}

	public HeadViewPageView(Context context,int category) {
		super(context);
		// TODO Auto-generated constructor stub
	
		this.context = context;
		this.category = category;
		initView();
	}

	

	private void initView() {
		// TODO Auto-generated method stub
		asyncHttpClient = new AsyncHttpClient();
		headCommedBeans = new ArrayList<HeadCommedBean>();

		xmlCacheHelper = new XmlCacheHelper(context);
		View mainView = View.inflate(context, R.layout.head_recommed_lay, this);
		pager = (ViewPager) mainView.findViewById(R.id.pager);
		indicator = (CirclePageIndicator)mainView.findViewById(R.id.indicator);

		pagerAdapter = new ImagePagerAdapter(headCommedBeans);

		pager.setAdapter(pagerAdapter);
		pager.setCurrentItem(0);
		pager.setOffscreenPageLimit(2);
		indicator.setViewPager(pager);
		//indicator.setSnap(true);
		pager.setOnTouchListener(new View.OnTouchListener() {
	      
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				arg0.getParent().requestDisallowInterceptTouchEvent(true);
				return false;
			}
	    });
		
		
		if(Toolkit.isAvaliable(context)){
			sendHttp();
		}else{
			headCommedBeans = (List<HeadCommedBean>) xmlCacheHelper.Xml2Object(HEAD_DATA);
		}
	}

	
	
	
	private void sendHttp() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("languageNum", App.localeCode+"");
		params.put("typeidNum", category+"");

		asyncHttpClient.post(App.HTTP_PATH+"ads_showAdsList.action?", params, new AsyncHttpResponseHandler(){

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);

				System.out.println("广告1返回信息");
				System.out.println(new String(arg2));
				
				HeadCommedHttpDataBean httpDataBean = new Gson().fromJson(new String(arg2), new TypeToken<HeadCommedHttpDataBean>(){}.getType());

				if(httpDataBean.getState()==200){
					headCommedBeans.clear();
					headCommedBeans.addAll(httpDataBean.getObjlistJson()) ;

					pagerAdapter.count = headCommedBeans.size();
					pagerAdapter.notifyDataSetChanged();

					xmlCacheHelper.Object2Xml(headCommedBeans, HEAD_DATA);

				}
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			}
		});
	}


	private class ImagePagerAdapter extends PagerAdapter {

		private List<HeadCommedBean> commedBeans;

		public int count;
		ImagePagerAdapter(List<HeadCommedBean> commedBeans) {
			this.commedBeans = commedBeans;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((View) object);
		}

		@Override
		public int getCount() {
			count = commedBeans.size();
			return count;
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			View imageLayout = View.inflate(context, R.layout.photo_view_item, null);

			ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.progressBar);
			if(!TextUtils.isEmpty(commedBeans.get(position).getAdsimage())){
 				Picasso.with(context).load(App.HTTP_PATH+commedBeans.get(position).getAdsimage())
				.error(R.drawable.detail_pic_loading)
				.placeholder(R.drawable.detail_loading_icon)
				.fit()
				.into(imageView)
				;
			}else{
				spinner.setVisibility(View.GONE);
			}
	
			((ViewPager) view).addView(imageLayout, 0);
			return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}


	}




}
